$(function() {
    // 保存用户信息
    let usrData = null;

    function getIndex() {
        Ajax(`get`, `/my/userinfo`, {}, function(res) {
            if (res.status) return layer.msg(res.message, { icon: 2 });
            //点击退出按钮的事件
            $(`#exit`).on(`click`, function() {
                layer.alert(`确定退出登录吗？`, {
                    title: `提示`,
                    icon: 0,
                    btn: [`确定`, `取消`],
                    yes: function(index) {
                        sessionStorage.removeItem(TOKEN);
                        location.href = `/login.html`;
                        layer.close(index);
                    },
                    btn2: function(index) {
                        layer.close(index)
                    }
                });
            });

            $(`iframe`).on(`load`, function() {
                const win = this.contentWindow;
                if (win.layui) {
                    win.$(`form`).on(`submit`, function(e) {
                        e.preventDefault();
                        const data = win.$(this).serialize();
                        let url = ``;
                        const formId = $(this).attr("id");
                        if (formId === `usr_info`) {
                            url = `/my/userinfo`;
                        } else if (formId === `usr_pwd`) {
                            url = `/my/updatepwd`;
                        } else return;
                        Ajax(`post`, url, data, function(res) {
                            if (res.status) return win.layer.msg(res.message);
                            win.layer.msg(`更新成功！`, { time: 1000 }, function() {
                                if (win.$(`input[name="nickname"]`).val()) {
                                    getIndex({ data: { nickname: win.$(`input[name="nickname"]`).val() } });
                                } else {
                                    window.top.location.replace(`/login.html`);
                                }
                            });
                        });
                    }).on(`reset`, function(e) {
                        if ($(this).attr("id") === `usr_info`) {
                            e.preventDefault();
                            win.$(`input[name="nickname"]`).val(``);
                            win.$(`input[name="email"]`).val(``);
                        }
                    });
                }
            });
            const $selfcenter = $(`#self-center`);
            const $short = $(`.short-nav`);
            const $left = $(`.left`);
            //给快捷下拉绑定与左侧导航对应的事件
            $short.find(`dd`).on(`click`, function() {
                $left.find(`.${this.classList[0]}`).addClass(`layui-this`).siblings().removeClass(`layui-this`).parents().siblings().removeClass(`layui-this`);
                $selfcenter.addClass(`layui-nav-itemed`);
            });
            //给左侧导航绑定点击与快捷下拉对应
            $left.find(`dd`).on(`click`, function() {
                $short.find(`dd`).removeClass(`layui-this`);
                $short.find(`dd`).each((index, ele) => {
                    if (ele.classList[0] === this.classList[0]) $(ele).addClass(`layui-this`);
                });
            });
            //点击首页清空其他的选中
            $(`#home`).on(`click`, function() {
                $(this).siblings().removeClass(`layui-nav-itemed`).find(`dd`).removeClass(`layui-this`);
                $short.find(`dd`).removeClass(`layui-this`);
            });
            //点击头像跳转更换头像页面
            $(`.user_pic`).on(`click`, function() {
                $(`.short-nav .avatar-short`).find(`a span`).trigger(`click`);
            });
            usrData = res;
            window.usrData = usrData;
            // 设置用户信息
            // 1.渲染用户名
            const nickname = res.data.nickname || res.data.username;
            $("#welcome").html(`${nickname}`).show();
            // 2.渲染头像
            let pic = res.data.user_pic;
            if (pic) {
                $(".user-info img").attr(`src`, pic).show();
                $("span.user_pic").hide();
            } else {
                pic = nickname[0].toUpperCase();
                $(".user-info>img").hide();
                $("span.user_pic").html(pic).css(`display`, `flex`);
            }
        });
    }
    getIndex();
    window.getIndex = getIndex;
})